package com.glela.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestPushModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.PushMessageService;
import com.glela.platform.util.ParameterUtils;

/**
 * 聊天消息控制类
 * @author zls
 *
 */
@Controller
@RequestMapping("/pushMessage")
@Scope("singleton")
public class PushMessageController {
	private static Logger logger = LoggerFactory.getLogger(PushMessageController.class);

	@Autowired
	private PushMessageService pushMessageService ;
	
	/**
	 * 根据 推送消息id 进行消息推送
	 * @param model pushMessageId 推送消息id 
	 * @return
	 */
	@RequestMapping(value = "/getPusMessageById", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getPusMessageById(@RequestBody RequestPushModel model) {
		try {
			if (ParameterUtils.validParams(model.getPushMessageId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return pushMessageService.getPusMessageById(model.getPushMessageId());
		} catch (Exception e) {
			logger.error("推送异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
	}
	
	/**
	 * 用户消息查询
	 * @param userId  type
	 * @return
	 */
	@RequestMapping(value = "/getUserMessage", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getUserMessage(@RequestBody RequestPushModel model) {
		try {
			if (ParameterUtils.validParams(model.getUserId(), model.getPageNum(), model.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return pushMessageService.getUserMessageByUserIdAndStatus(model);
		} catch (Exception e) {
			logger.error("查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
	}
	
	/**
	 * 列表数据
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getUserMessageList", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getUserMessageList(@RequestBody RequestPushModel model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return pushMessageService.getUserMessageListByUserIdAndStatus(model.getUserId());
		} catch (Exception e) {
			logger.error("查询异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
	}
	
	
	/**
	 * 根据 推送消息id 进行消息推送
	 * @param response
	 * @param request
	 * @param pushMessageId  推送消息id 
	 */
	/*@RequestMapping(value = "getPusMessageById", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void getPusMessageById(HttpServletResponse response, HttpServletRequest request) {
		try {
			List<Valid> validlist = new ArrayList<Valid>();
			validlist.add(new Valid("推送消息id", "pushMessageId", "required|isNaturalNoZero"));

			PushMessageVo pushMessageVo = (PushMessageVo) toBean(request, PushMessageVo.class, validlist);
			if (pushMessageVo.getCode() != 100) {
				writeJsonObject(response, pushMessageVo.getCode(), pushMessageVo.getMsg(), null);
				return;
			}
			pushMessageVo = pushMessageService.getPusMessageById(pushMessageVo.getPushMessageId());

			if (pushMessageVo.getCode() != 100) {
				writeJsonObject(response, pushMessageVo.getCode(), pushMessageVo.getMsg(), null);
			} else {
				writeJsonObject(response, 100, "操作成功", pushMessageVo);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			writeError(response, e);
		}
	}*/

	/**
	 * <dl>
	 * <dt>获取未读消息数</dt>
	 * </dl>
	 * @param model
	 * @return ResponseEntity<?>
	 * @author zls
	 * @date 2017年7月6日 下午7:18:21
	 */
	@RequestMapping(value = "/getUnreadMessageNums", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getUnreadMessageNums(@RequestBody RequestPushModel model) {
		try {
			if (ParameterUtils.validParams(model.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(pushMessageService.getUnreadMessageNums(model.getUserId(), null, 2)), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("查询未读消息异常：{}-{}", JSONObject.toJSONString(model), e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
}
